package com.guchenbo.example.thread;

/**
 * @Author guchenbo
 * @Date 2018/10/21.
 */
public class TestJoin {

    public static void main(String[] args) throws InterruptedException {
        TestJoin demo = new TestJoin();
        //                demo.test1();
        //                demo.test2();
        //        demo.test3();
        //        demo.test4();
        demo.test5();
    }

    private void test5() throws InterruptedException {
        ThreadC t1 = new ThreadC();
        t1.join();
        t1.start();

        System.out.println("dddddd");
    }

    private void test2() throws InterruptedException {
        ThreadB t1 = new ThreadB();
        //        synchronized (t1) {
        t1.start();

        t1.join();

        System.out.println("dddddd");
        //        }
    }

    private void test3() throws InterruptedException {
        ThreadB t1 = new ThreadB();
        t1.join();
        synchronized (t1) {
            t1.start();

            t1.join();
            t1.wait();
            System.out.println("dddddd");
        }
    }

    private void test1() {

        ThreadA t1 = new ThreadA();
        Thread t = new Thread(t1);
        synchronized (t1) {
            System.out.println(Thread.currentThread().getName() + " 获得锁 ");
            System.out.println(Thread.currentThread().getName() + " 启动线程t ");
            t.start();
            try {
                t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //            try {
            //                Thread.sleep(2000);
            //            } catch (InterruptedException e) {
            //                e.printStackTrace();
            //            }
        }
        System.out.println(Thread.currentThread().getName() + " 释放锁 ");
    }

    class ThreadA implements Runnable {

        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + " start ");
            synchronized (this) {
                System.out.println(Thread.currentThread().getName() + " done ");
            }
        }
    }

    class ThreadB extends Thread {

        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + " start ");
            synchronized (this) {
                System.out.println(Thread.currentThread().getName() + " done ");
            }
        }
    }

    class ThreadC extends Thread {

        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + " start ");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(Thread.currentThread().getName() + " done ");
        }
    }
}
